<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="mainUser">
<title>All User Period Statistics</title>
</head>
<body>
	<div id="container">
		<div>
			<h2>All User Period Statistics:</h2>
			<g:select name="intPeriod" from="${1..12}" value="${intPeriod}"/>

			<table id="hor-minimalist">
				<thead>
					<tr>
						<th scope="col">No.</th>
						<th scope="col">User</th>
						<th scope="col">Order Count</th>
						<th scope="col">This Period Money</th>
						<th scope="col">Adjust Money</th>
						<th scope="col">Total Money</th>
						<th scope="col">User Bill Money</th>
					</tr>
				</thead>
				<tbody>
					<g:each in="${userBillPeriodHistoryList}"
						var="userBillPeriodHistory" status="i">
						<g:set var="userBillPeriodHistoryId" value="${userBillPeriodHistory.id}"  />
						<g:set var="userId" value="${userBillPeriodHistory.user.id}"  />
						<tr>
							<td>
								${i+1}
							</td>
							<td>
								<g:link controller="userOrderHistory" action="userOrderPeriodHistory" id="${userId }" value="">${userBillPeriodHistory.user}</g:link>
							</td>
							<td>
								${userBillPeriodHistory.orderCount}
							</td>
							<td id="thisPeriodMoney${userBillPeriodHistoryId}">
								${userBillPeriodHistory.thisPeriodMoney}
							</td>
							<td>
								<g:field name="adjustMoney${userBillPeriodHistoryId}" type="text" value="${userBillPeriodHistory.lastOweMoney}"/>
								<g:field name="adjustMoneyBtn${userBillPeriodHistoryId}" _id="${userBillPeriodHistoryId}" class="btn adjustMoney" type="button" value="Update"/>
							</td>
							<td id="totalMoney${userBillPeriodHistoryId}">
								${userBillPeriodHistory.totalMoney}
							</td>
							<td>
								<g:field name="billMoney${userId}" type="text" value="${userBillPeriodHistory.user.oweMoney}"/>
								<g:field name="set0billMoney${userId}" _id="${userId}" class="btn set0billMoney" type="button" value="Set to Zero"/>
								<g:field name="updateBillMoney${userId}" _id="${userId}" class="btn billMoney" type="button" value="Update"/>
							</td>
						</tr>
					</g:each>
				</tbody>
			</table>
		</div>
	</div>
	<jq:jquery>
		$('.adjustMoney').click(function(){
			var id = $(this).attr('_id');
			var adjustMoney = $('#adjustMoney' + id).val();
			adjustMoney = adjustMoney == ''? 0 : adjustMoney;
			$.ajax({
				type : 'get',
				url : '/orderLunch/userBillPeriodHistory/ajustOweMoney',
				data : {id:id, lastOweMoney:adjustMoney},
				success: function(data) {
					$('#totalMoney' + id).html(parseFloat($('#thisPeriodMoney' + id).html()) + parseFloat(adjustMoney));
				}
			});
		});
		$('.billMoney').click(function(){
			var id = $(this).attr('_id');
			var billMoney = $('#billMoney' + id).val();
			$.ajax({
				type : 'get',
				url : '/orderLunch/orderUser/ajustOweMoney',
				data : {id : id, oweMoney : billMoney},
				success: function(data) {
					alert('success');
				}
			});
		});
		$('.set0billMoney').click(function(){
			var id = $(this).attr('_id');
			$.ajax({
				type : 'get',
				url : '/orderLunch/orderUser/set0billMoney',
				data : {id : id},
				success: function(data) {
					$('#billMoney' + id).val(0);
				}
			});
		});
		$('#intPeriod').change(function(){
			var intPeriod = $(this).val();
			location.href = '/orderLunch/userOrderHistory/allUserPeriodStats?intPeriod=' + intPeriod
		});
	</jq:jquery>
</body>